
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutterapp/stream/AppPage.dart';
import 'package:flutterapp/stream/StreamAppPage.dart';

class StreamPage  extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return StreamPageState();
  }
}

class StreamPageState extends State<StreamPage>{
  // 初始化一个单订阅的Stream controller
//  StreamController mController ;
  @override
  void initState() {
    super.initState();
//    mController = StreamController();
  }

  @override
  Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(title: Text("StreamPage")),
       body: Center(
         child: Column(
           children: <Widget>[
             RaisedButton(
               onPressed: () {
                createStream();
               },
               child: Text("单订阅"),
             ),
             RaisedButton(
               onPressed: () {
                 createBroadcastStream();
               },
               child: Text("多订阅"),
             ),RaisedButton(
               onPressed: () {
                 Navigator.push(
                     context,
                     MaterialPageRoute(
                         builder: (BuildContext context) => AppPage()));
               },
               child: Text("官方demo"),
             ) ,
             RaisedButton(
               onPressed: () {
                 Navigator.push(
                     context,
                     MaterialPageRoute(
                         builder: (BuildContext context) => StreamAppPage()));
               },
               child: Text("改造后的demo"),
             )
           ],
         ),
       ),
     );
  }




  void createStream() {
    StreamController mController  =  StreamController();
    //获取到StreamController创建的Stream对象
    Stream mStream = mController.stream;
    //初始化监听
     mStream.listen((data) {
      print("---->$data");
    }, onError: (err) {
      print('error happen： $err');
    }, onDone: () {
      print('Mission complete!');
    });
    //往Stream中添加数据
    mController.sink.add("String event");
    mController.sink.add(1234);
    mController.sink.add({'name': '捞捞浩', '性别': '男'});
    // 向Stream中添加error信息
    mController.sink.addError('something wrong here!');
    //释放资源
    mController.close();


    ///listen方法简介
    ///     onData 是接收到数据的处理,必须要实现的方法
    ///     onError 流发生错误时候的处理
    ///     onDone 流完成时候调取
    ///     cancelOnError 发生错误的时候是否立马终止
  }




  void createBroadcastStream() {
    // 初始化一个单订阅的Stream controller
    final StreamController mController = StreamController.broadcast();
    //获取到StreamController创建的Stream对象
    Stream mStream = mController.stream;
    //初始化监听
    mStream.listen((data) {
      print("subscriptionOne--->$data");
    }, onError: (err) {
      print('subscriptionOne error happen： $err');
    }, onDone: () {
      print('subscriptionOne Mission complete!');
    });
    //初始化监听 返回 StreamSubscription
    mStream.listen((data) {
      print("subscriptionTwo--->$data");
    }, onError: (err) {
      print('subscriptionTwo error happen： $err');
    }, onDone: () {
      print('subscriptionTwo Mission complete!');
    });
    //往Stream中添加数据
    mController.sink.add("String event");
    mController.sink.add(1234);
    mController.sink.add({'name': '捞捞浩', '性别': '男'});
    // 向Stream中添加error信息
    mController.sink.addError('something wrong here!');
    mController.sink.add(1234);
//    subscriptionTwo.cancel();
    //释放资源
    mController.close();
  }

}